package cz.jm.web.manhunter.client;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;

import cz.jm.web.jmlib.client.base.Presenter;
import cz.jm.web.jmlib.client.base.View;
import cz.jm.web.manhunter.client.authentication.LoginPresenter;
import cz.jm.web.manhunter.client.registration.RegistrationPresenter;
import cz.jm.web.manhunter.client.services.UserDataServiceAsync;

public class AppController {

	interface Display extends View {
		HasClickHandlers getProfile();

		HasClickHandlers getSkills();

		HasClickHandlers getAbout();

		HasClickHandlers getLoggIn();

		HasClickHandlers getRegistration();

		HasChangeHandlers getLanguageSelector();

		LayoutPanel getWorkingArea();

		String getLanguage();
		
	}

	private UserDataServiceAsync userDataService;
	private HandlerManager eventBus;
	private Display view;

	private Presenter actualPresenter = null;

	public AppController(UserDataServiceAsync userDataService, HandlerManager eventBus, Display view) {
		this.userDataService = userDataService;
		this.eventBus = eventBus;
		this.view = view;

		bind();
	}

	public void go(RootLayoutPanel rootPanel) {
//		String regKey = Window.Location.getParameter(SharedConstants.REGISTRATION_KEY);
//		String userId = Window.Location.getParameter(SharedConstants.USER_ID);
//		if (regKey != null && userId != null) {
//			userDataService.confirmUserRegistration(Long.parseLong(userId), regKey, 
//					new AsyncCallback<Void>() {
//
//						@Override
//                        public void onFailure(Throwable caught) {
//	                        // TODO Auto-generated method stub
//	                        
//                        }
//
//						@Override
//                        public void onSuccess(Void result) {
//	                        // TODO Auto-generated method stub
//	                        
//                        }} );
//		}

		rootPanel.add(view.asWidget());
	}

	public void bind() {
		view.getProfile().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
//				DialogWidget w = new DialogWidget();
//				view.setContent(w.asWidget());
			}
		});

		view.getSkills().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Window.alert("Ahoj !!!");
			}
		});


		view.getAbout().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				cz.jm.web.jmlib.client.dialog.registration.RegistrationView v = 
						new cz.jm.web.jmlib.client.dialog.registration.RegistrationView();
				//v.setIcon(ResorcesInfo.getIconKeys());
				activate(new RegistrationPresenter(userDataService, v));
			}
		});

		view.getLoggIn().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				cz.jm.web.jmlib.client.dialog.login.LoginView v = 
						new cz.jm.web.jmlib.client.dialog.login.LoginView();
				//v.setIcon(ResorcesInfo.getIconKeys());
				activate(new LoginPresenter(userDataService, v));
			}
		});

		view.getRegistration().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
//				activate(new RegistrationPresenter(userDataService, new RegistrationView()));

			}
		});

		view.getLanguageSelector().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				UrlBuilder url = Window.Location.createUrlBuilder();
				String param[] = new String[1];
				param[0] = view.getLanguage();
				url.setParameter("locale", param);
				Window.Location.replace(url.buildString());
			}
		});

	}

	protected void activate(Presenter presenter) {
		actualPresenter = presenter;
		view.getWorkingArea().clear();
		actualPresenter.go(view.getWorkingArea());
	}
}
